﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hero : MonoBehaviour, ICameraFollowTarget, IPlayerHero
{


    public EMoveOri GetMoveOri()
    {
        if (Actor != null)
        {
            return Actor.NowMoveOri;
        }
        return EMoveOri.E_MID;
    }

    public Vector2 GetPosition()
    {
        return new Vector2(this.transform.position.x, this.transform.position.y);
    }

    public void SwitchGun() {
        nowGunIndex_ = nowGunIndex_ + 1;
        if (nowGunIndex_ > this.listOfGuns_.Count - 1) { this.nowGunIndex_ = 0; }
        Actor.SetGun(listOfGuns_[nowGunIndex_]);
    }
    private int nowGunIndex_ = 2;
    private List<GunBase> listOfGuns_ = new List<GunBase>();
    public ActorHuman Actor = null;
    // Start is called before the first frame update
    void Start()
    {
        listOfGuns_.Add(new GunOne());
        listOfGuns_.Add(new GunTwo());
        listOfGuns_.Add(new GunThree());
        ICmdSender sender = this.gameObject.AddComponent<PlayerCtrl>();
        sender.SetCmdReader(Actor);
        Actor.SetGun(listOfGuns_[listOfGuns_.Count -1]);
        Actor.MotionState = this.gameObject.GetComponent<MotionStateMgr>();
    }


    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == TagsMgr.ENEMY)
        {
            this.Dead();
        }
    }

    public void Live()
    {
        // body live
        this.GetComponent<PlayerCtrl>().isAKeyDown_ = false;
        this.GetComponent<PlayerCtrl>().isDKeyDown_ = false;
        this.GetComponent<Rigidbody2D>().WakeUp();
        this.gameObject.SetActive(true);
        this.isDead_ = false;
        this.gameObject.transform.localPosition = new Vector3(4,0.73f, this.gameObject.transform.localPosition.z);
    }

    private bool isDead_ = false;

    public void Dead()
    {
        if (this.isDead_ == false) {
            // time scale
            // body dead
            //    body hide
            //    create body
            this.GetComponent<Rigidbody2D>().Sleep();
            this.gameObject.SetActive(false);

            this.mgr_.GameOver(-1);

            var pos = this.transform.localPosition;
            int horiValue = UnityEngine.Random.Range(0, 2) - 1;
            PrefabsMgr.GetInstance().Send(
                PrefabsMgr.TYPE_ENEMY_BODY,
                new WakeUpConfigDeadBody(pos, new Vector2(1 * horiValue, 0), SpritesMgr.GetInstance().SpPlayerDeadBody, true)
                );
            // delayForGameOver
            this.isDead_ = true;
        }

    }

    private IGameManager mgr_ = null;
    public void SetGameManager(IGameManager mgr)
    {
        mgr_ = mgr;
    }
}
